package com.zpp.wedding_admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zpp.wedding_admin.entity.Join;
import com.zpp.wedding_admin.service.IJoinService;
import com.zpp.wedding_admin.utils.LQWUtils;
import com.zpp.wedding_common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "参加活动管理")
@CrossOrigin
@RestController
@RequestMapping("/weddingadmin/join")
public class JoinController {

    @Autowired
    private IJoinService joinService;

    @ApiOperation(value = "用户参加一个活动")
    @GetMapping("joinActivity/{userId}/{activityId}")
    public R joinActivity(@PathVariable Integer userId, @PathVariable Integer activityId) {
        boolean b = joinService.joinAdd(activityId, userId);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "获取所有活动")
    @GetMapping("getAllActivity")
    public R getAllActivity() {
        List<Join> joinList = joinService.list(null);
        return R.ok().data("joinList", joinList);
    }

    @ApiOperation(value = "通过活动id获取用户所有参加的活动信息")
    @GetMapping("getActivityById/{id}")
    public R getActivityById(@PathVariable Integer id) {
        List<Join> joinList = joinService.getActivityById(id);
        return R.ok().data("joinList", joinList);
    }

    @ApiOperation(value = "通过id获取一个活动信息")
    @GetMapping("getOneActivity/{id}")
    public R getOneActivity(@PathVariable Integer id) {
        Join join = joinService.getById(id);
        return R.ok().data("join", join);
    }

    @ApiOperation(value = "判断是否参加了当前活动")
    @GetMapping("isJoin/{userId}/{activityId}")
    public R isJoin(@PathVariable Integer userId, @PathVariable Integer activityId) {
        LambdaQueryWrapper<Join> lqw = LQWUtils.getLqw(Join.class);
        lqw.eq(Join::getUserId, userId).eq(Join::getActivityId, activityId);
        Join join = joinService.getOne(lqw);
        if ( join != null ) {
            return R.ok().data("isJoin", true);
        } else {
            return R.ok().data("isJoin", false);
        }
    }

    @ApiOperation(value = "获取参与该活动的所有用户")
    @GetMapping("getAllUser/{id}")
    public R getAllUser(@PathVariable Integer id) {
        List<Join> joinList = joinService.getAllUser(id);
        return R.ok().data("joinList", joinList).data("total", joinList.size());
    }
}
